/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.gui;

import java.awt.Choice;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.border.TitledBorder;

/**
 *
 * @author DK
 */
public class LearningOptionViewPanel extends JPanel implements ActionListener, ItemListener{
    
    MainFrame mf;
    Controller ctrl;
    JButton start;
    Choice learningType;
    private JTextField entThreshold;
    private GridBagLayout gbag = new GridBagLayout();
    
    public LearningOptionViewPanel(MainFrame mf){
        this.setLayout(gbag);
        this.mf = mf;
        this.ctrl = this.mf.getController();
        
        
//        this.setBorder(new TitledBorder("Learning Option"));
        this.setBorder(BorderFactory.createEtchedBorder());
        this.learningType = new Choice();
        this.learningType.add("DEFAULT");
        this.learningType.add("USE_CONJUNTION AND DISJUNCTION");
        this.learningType.addItemListener(this);
        this.entThreshold = new JTextField();
        this.entThreshold.addKeyListener(new KeyAdapter(){
            public void keyTyped(KeyEvent e){
                if(e.getKeyChar() == KeyEvent.VK_ENTER){
                    String s = entThreshold.getText();
                    double thre = Double.parseDouble(s);
                    ctrl.setThreshold(thre);
                }
            }
        });
        
        JLabel learnTypeLabel = new JLabel();
        learnTypeLabel.setFont(new java.awt.Font("맑은 고딕", 1, 12));
        learnTypeLabel.setText("Learning Type");
        
        JLabel eThresholdLabel = new JLabel();
        eThresholdLabel.setFont(new java.awt.Font("맑은 고딕", 1, 12));
        eThresholdLabel.setText("Entropy Threshold :");
        
        JLabel emptyLabel = new JLabel();
        emptyLabel.setText("                    ");
        
        this.start = new JButton("Start");        
        this.start.addActionListener(this);
//        this.add(start);
        
        this.insert(learnTypeLabel, 0, 0, 4, 1);
        this.insert(this.learningType, 0, 1, 4, 1);
        this.insert(eThresholdLabel, 0, 2, 2, 1);
        this.insert(this.entThreshold, 2, 2, 2, 1);
        this.insert(emptyLabel, 0, 3, 4, 1);        
        this.insert(this.start, 0, 4, 4, 1);        
    }
    
    private void insert(Component cmpt, int x, int y, int w, int h){
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = w;
        gbc.gridheight = h;
        this.gbag.setConstraints(cmpt, gbc);
        this.add(cmpt);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object o = e.getSource();
        
        if(o==this.start){
            
            if(CurrentMainFrameStatus.TARGETCLASS_SELECTED && CurrentMainFrameStatus.TARGETPROPERTY_SELECTED){
                try {
                    this.mf.outputTextArea.append("Learning Algorithm Start\n");
                    this.ctrl.defineSDTLearner(CurrentMainFrameStatus.TARGET_PROPERTY_USED);
                } catch (IOException ex) {
                    Logger.getLogger(LearningOptionViewPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }else if(CurrentMainFrameStatus.TARGETCLASS_SELECTED && CurrentMainFrameStatus.TARGETINSTANCE_SELECTED){
                try {
                    this.mf.outputTextArea.append("Learning Algorithm Start\n");
                    this.ctrl.defineSDTLearner(CurrentMainFrameStatus.TARGET_INSTANCE_USED);
                } catch (IOException ex) {
                    Logger.getLogger(LearningOptionViewPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }else{
                if(!CurrentMainFrameStatus.TARGETCLASS_SELECTED){
                    JOptionPane.showMessageDialog(this, "Set the Target Class", "Error", JOptionPane.WARNING_MESSAGE); 
                }else if(!CurrentMainFrameStatus.TARGETPROPERTY_SELECTED || !CurrentMainFrameStatus.TARGETINSTANCE_SELECTED){
                    JOptionPane.showMessageDialog(this, "Set the Target Property or Instance", "Error", JOptionPane.WARNING_MESSAGE); 
                }else if(!CurrentMainFrameStatus.TARGETINSTANCE_SELECTED){
                    JOptionPane.showMessageDialog(this, "Set the Target Instance", "Error", JOptionPane.WARNING_MESSAGE); 
                }
            }
        }
        
        
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        int index = this.learningType.getSelectedIndex();
//        System.out.println(index);
        this.ctrl.setLearningType(index);
    }
    
}
